package com.zr.d_c.serviceimpl;

import com.zr.d_c.dao.AdminDao;
import com.zr.d_c.dao.CustomerDao;
import com.zr.d_c.dao.RoleDao;
import com.zr.d_c.dao.SellerDao;
import com.zr.d_c.dao.UserDao;
import com.zr.d_c.daoimpl.AdminDaoImpl;
import com.zr.d_c.daoimpl.CustomerDaoImpl;
import com.zr.d_c.daoimpl.RoleDaoImpl;
import com.zr.d_c.daoimpl.SellerDaoImpl;
import com.zr.d_c.daoimpl.UserDaoImpl;
import com.zr.d_c.model.Role;
import com.zr.d_c.model.Customer;
import com.zr.d_c.model.Seller;
import com.zr.d_c.model.User;
import com.zr.d_c.service.UserInfoService;

import net.sf.json.JSONObject;

public class UserInfoServiceImpl implements UserInfoService {

	UserDao userdao = new UserDaoImpl();
	RoleDao roledao = new RoleDaoImpl();
	CustomerDao customerdao =new CustomerDaoImpl();
	AdminDao admindao = new AdminDaoImpl();
	SellerDao sellerdao = new SellerDaoImpl();
	public JSONObject getCurrentUserInfoByUnameAndUpsw(User u) {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject();
		if(u.getUname() != null){
			Role r = roledao.selectRoleByRid(u.getRid());
			if(r.getRname().equals("管理员")){
				json = JSONObject.fromObject(admindao.selectAdminByUid(u.getUid()));
			}else if(r.getRname().equals("商家")){
				json = JSONObject.fromObject(sellerdao.selectSellerByUid(u.getUid()));
			}else if(r.getRname().equals("客户")){
				json = JSONObject.fromObject(customerdao.selectCustomerByUid(u.getUid()));
			}
			return json;
		}
		return null;
	}
	public String getCurrentUserNameByUnameAndUpsw(User u) {
		// TODO Auto-generated method stub
		String name = "";
		if(u.getUname() != null){
			Role r = roledao.selectRoleByRid(u.getRid());
			if(r.getRname().equals("管理员")){
				name = admindao.selectAdminByUid(u.getUid()).getAname();
			}else if(r.getRname().equals("商家")){
				name = sellerdao.selectSellerByUid(u.getUid()).getSname();
			}else if(r.getRname().equals("客户")){
				name = customerdao.selectCustomerByUid(u.getUid()).getCname();
			}
			return name;
		}
		return null;
	}
	@Override
	public int getCurrentUserCidByUid(int uid) {
		// TODO Auto-generated method stub
		return customerdao.selectCustomerByUid(uid).getCid();
	}
	@Override
	public int getSellerSidByUid(int uid) {
		// TODO Auto-generated method stub
		return sellerdao.selectSellerByUid(uid).getSid();
	}
	@Override
	public boolean updateSinfo(Seller seller) {
		SellerDao sellerdao = new SellerDaoImpl();
		
		return sellerdao.updateSinfo(seller);
	}
	@Override
	public boolean deleteSinfo(int sid) {
		SellerDao sellerdao = new SellerDaoImpl();
		return sellerdao.deleteSeller(sid);
	}
	@Override
	public boolean addSeller(Seller seller) {
	   SellerDao sellerdao = new SellerDaoImpl();
		return  sellerdao.addSeller(seller);
	}
	@Override
	public int modifyCurrentUserInfoByUid(Customer c) {
		// TODO Auto-generated method stub
		return customerdao.updateCinfoByUid(c);
	}
	@Override
	public int addCustomerInfo(Customer c) {
		// TODO Auto-generated method stub
		return customerdao.insertCustomerInfo(c);
	}

	}
 

